#include "PaletteShower.h"
#include <QDebug>


PaletteShower::PaletteShower(QWidget *parent) : QWidget(parent)
{
    this -> setFixedSize(344, 242);
    horizontalLayout = new QHBoxLayout(this);
    graphicsView1 = new QGraphicsView(this);
	graphicsView2 = new QGraphicsView(this);
	scene = new ImageScene;
	scene2 = new ImageScene;

    horizontalLayout->addWidget(graphicsView1);


    horizontalLayout->addWidget(graphicsView2);

	graphicsView1->setMouseTracking(true);
	graphicsView2->setMouseTracking(true);
	graphicsView1->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
	graphicsView1->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
	graphicsView2->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
	graphicsView2->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
	graphicsView1 -> setStyleSheet("padding: 0px; border: 0px;");
	graphicsView2 -> setStyleSheet("padding: 0px; border: 0px;");

	scene -> setPaletteShower(this);
	scene2 -> setPaletteShower(this);
}

PaletteShower::~PaletteShower()
{
    delete horizontalLayout;
    delete graphicsView1;
    delete graphicsView2;
	delete scene;
	delete scene2;
}

void PaletteShower::updateImage()
{

	QImage img = QImage(160, 224, QImage::Format_RGB888);
	QImage img2 = QImage(160, 224, QImage::Format_RGB888);

	QPainter painter(&img);
	QPainter painter2(&img2);

	// 反走样
	painter.setRenderHint(QPainter::Antialiasing, true);
	painter2.setRenderHint(QPainter::Antialiasing, true);
	// 设置画笔颜色、宽度
	painter.setPen(QPen(QColor(0, 0, 0), 1));
	painter2.setPen(QPen(QColor(0, 0, 0), 1));
	// 设置画刷颜色

	for(uint8_t i = 0; i < 32; i++)
	{
		for(uint8_t j = 0; j < 8; j++)
		{
            uint8_t order = i + (j * 32);
			painter.setBrush(QColor(palette[order].r, palette[order].g, palette[order].b));
			painter.drawRect(j * 20, i * 7, 20, 7);
			painter2.setBrush(QColor(palette[order].a, palette[order].a, palette[order].a));
			painter2.drawRect(j * 20, i * 7, 20, 7);
		}
	}

	painter.setPen(QPen(QColor(255, 255, 0), 2));
	painter2.setPen(QPen(QColor(255, 255, 0), 2));
	painter.setBrush(QColor(palette[pass_color_id].r, palette[pass_color_id].g, palette[pass_color_id].b));
	painter.drawRect(pass_color_id / 32 * 20, pass_color_id % 32 * 7, 20, 7);
	painter2.setBrush(QColor(palette[pass_color_id].a, palette[pass_color_id].a, palette[pass_color_id].a));
	painter2.drawRect(pass_color_id / 32 * 20, pass_color_id % 32 * 7, 20, 7);

	painter.setPen(QPen(QColor(255, 0, 0), 2));
	painter2.setPen(QPen(QColor(255, 0, 0), 2));
	painter.setBrush(QColor(palette[pick_color_id].r, palette[pick_color_id].g, palette[pick_color_id].b));
	painter.drawRect(pick_color_id / 32 * 20, pick_color_id % 32 * 7, 20, 7);
	painter2.setBrush(QColor(palette[pick_color_id].a, palette[pick_color_id].a, palette[pick_color_id].a));
	painter2.drawRect(pick_color_id / 32 * 20, pick_color_id % 32 * 7, 20, 7);

	graphicsView1->resize(img.width(), img.height());
	graphicsView2->resize(img2.width(), img2.height());

	scene->addPixmap(QPixmap::fromImage(img));
	scene2->addPixmap(QPixmap::fromImage(img2));

	graphicsView1->setScene(scene);
	graphicsView1->adjustSize();
	graphicsView1->show();

	graphicsView2->setScene(scene2);
	graphicsView2->adjustSize();
	graphicsView2->show();
}


void PaletteShower::setPalette(Palette& palette)
{
	this -> palette = palette;
}


ImageScene::ImageScene(): QGraphicsScene()
{
}

ImageScene::ImageScene(QObject* parent): QGraphicsScene(parent)
{

}

ImageScene::~ImageScene()
{

}

void ImageScene::mousePressEvent(QGraphicsSceneMouseEvent* event)
{
	int pressed_x = (int) event -> scenePos().x();
	int pressed_y = (int) event -> scenePos().y();
	uint8_t j = pressed_x / 20;
    uint8_t i = pressed_y / 7;
	pick_color_id = i + (j * 32);

	if(paletteShower -> pick_color_id != pick_color_id)
	{
        paletteShower->pick_color_id = pick_color_id;
        paletteShower->updateImage();
    }
}

void ImageScene::mouseMoveEvent(QGraphicsSceneMouseEvent* event)
{
	int move_x = (int) event -> scenePos().x();
	int move_y = (int) event -> scenePos().y();
	uint8_t j = move_x / 20;
	uint8_t i = move_y / 7;
	pass_color_id = i + (j * 32);

	if(paletteShower -> pass_color_id != pass_color_id)
	{
        paletteShower -> pass_color_id = pass_color_id;
        paletteShower -> updateImage();
	}
}

void ImageScene::setPaletteShower(PaletteShower* paletteShower_)
{
	this -> paletteShower = paletteShower_;
}

